<script setup>
import { useRouter } from 'vue-router'
import { reactive, ref } from 'vue'
import { useStore } from 'vuex'
import store from '@/store/index.js'
import cache from '@/util/cache.js'
import xcRequest from '@/service/index.js'
const router = useRouter()
const sotre = useStore()
const form = ref({
  uName: '',
  uPwd: ''
})
const saveInfo = cache.getCache('jxcms-saveInfo')
if (saveInfo) {
  form.value = saveInfo
}
const isSaveLocal = ref(true)
const ruleFormRef = ref()
const rules = reactive({
  uName: [
    { required: true, message: '必须要输入用户名', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在6到18位', trigger: 'blur' }
  ],
  uPwd: [
    { required: true, message: '必须要输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在6到18位', trigger: 'blur' }
  ]
})
const goRegisterPage = () => {
  router.push('/register')
}
const handLoginClick = () => {
  //表单验证
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      store.dispatch('loginUser', form.value)
      if (isSaveLocal.value) {
        cache.setCache('jxcms-saveInfo', form.value)
      } else {
        cache.deleteCache('jxcms-saveInfo')
      }
    }
  })
}
</script>

<template>
  <div class="login">
    <div class="login-form">
      <div class="form-left">
        <div>
          <h1>江西省水利管理系统</h1>
        </div>
      </div>
      <div class="form-right">
        <el-form
          :model="form"
          :rules="rules"
          ref="ruleFormRef"
          label-position="top"
          label-width="120px"
          class="demo-ruleForm"
          hide-required-asterisk
        >
          <div class="title">
            <h1>欢迎登录</h1>
          </div>

          <el-form-item label="用户名" prop="uName">
            <el-col :span="16">
              <el-input
                autocomplete="off"
                suffix-icon="Avatar"
                size="large"
                v-model="form.uName"
              />
            </el-col>
          </el-form-item>

          <el-form-item label="密码" prop="uPwd">
            <el-col :span="16">
              <el-input
                type="password"
                autocomplete="off"
                suffix-icon="Lock"
                size="large"
                v-model="form.uPwd"
              />
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-col :span="12">
              <el-radio-group class="ml-4">
                <el-checkbox
                  v-model="isSaveLocal"
                  label="记住密码"
                  size="large"
                />
              </el-radio-group>
            </el-col>
            <el-col :span="12">
              <div class="register" @click="goRegisterPage">注册帐号</div>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-col :span="16">
              <el-button class="btn" type="primary" @click="handLoginClick"
                >登录</el-button
              >
            </el-col>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
:deep(.el-checkbox.el-checkbox--large .el-checkbox__inner) {
  border-radius: 50%;
}
:deep(.el-checkbox.el-checkbox--large .el-checkbox__label) {
  color: rgba(204, 204, 204, 1);
}
:deep(.el-input .el-input__icon) {
  font-size: 22px;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: url(../../assets/img/bg.png) no-repeat;
  background-size: 1000px;
  background-position: -50px -130px;

  .login-form {
    display: flex;
    width: 1156px;
    height: 630px;
    background: #f5fafe;
    border-radius: 10px;
    .form-left {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 721px;
      height: 100%;
      color: rgba(42, 130, 228, 1);
      font-size: 24px;
    }
    .form-right {
      flex: 1;
      .title {
        margin: 140px 0 30px 0;
        color: rgba(66, 76, 116, 1);
        h1 {
          font-weight: 500;
        }
      }
      .register {
        cursor: pointer;
        color: rgba(204, 204, 204, 1);
      }
      .btn {
        width: 100%;
        height: 45px;
        font-size: 18px;
        span {
          width: 100%;
          text-align: center;
        }
      }
    }
  }
}
</style>
